package org.lanqiao.oa.visitor.impl;

import org.lanqiao.oa.composite.PermissionNode;
import org.lanqiao.oa.dao.PermissionDao;
import org.lanqiao.oa.dto.PermissionDTO;
import org.lanqiao.oa.visitor.PermissionVisitor;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

public abstract class AbstractVisitorClass implements PermissionVisitor {

    abstract PermissionDao permissionDao();

    /**
     * 使用模板方法模式定义好执行的流程
     * @param permissionDTO
     * @throws IOException
     */
    public void templeMethod(PermissionDTO permissionDTO) throws IOException {
        baseVisit(permissionDTO);
        visit(permissionDTO);
    }

    private void baseVisit(PermissionDTO permissionDTO) throws IOException {
        //获取子权限
        List<PermissionDTO> sonPermiissionById = permissionDao().getSonPermiissionById(permissionDTO.getId());
        //把子权限转换为权限树
        List<PermissionNode> permissionNodes = new ArrayList<>(sonPermiissionById.size());
        sonPermiissionById.forEach(t -> {
            permissionNodes.add(t.copy(PermissionNode.class));
        });
        //使用组合模式判断是否有子权限
        if(permissionNodes.size()>0){
            permissionNodes.forEach(t -> {
                t.accept(this);
            });
        }

    }


}
